import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

import IndexView from '@/views/indexView.vue'


import OwnerView from '@/views/OwnerView.vue'
import DepartmentView from '@/views/DepartmentView.vue'
import ReapairerView from '@/views/ReapairerView.vue'

import RepairTaskView from '@/views/RepairTaskView.vue'
import LoginView from '@/views/LoginView.vue'
import ChooseView from '@/views/ChooseView.vue'

import RepairerLoginView from '@/views/RepairLoginView.vue'
import RepairHomeView from '@/views/RepairHomeView.vue'
import RepairView from '@/views/RepairView.vue'
import { ElMessage } from 'element-plus'
import { useTokenStore } from '@/stores/token';
import RepairView1 from "@/views/RepairView1.vue";
import Peijian from "@/views/peijian.vue";
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/login',
      name:'login',
      component:LoginView
    },{
      path: '/chooseLogin',
      component: ChooseView
    },{
      path: '/repairerLogin',
      component: RepairerLoginView
    },{
      name: 'repairehome',
      component: RepairHomeView,
      redirect: '/repair',
      children: [
        {
          path: '/repair',
          component: RepairView,
          // meta: { requiresAuth: true, roles: ['repairer'] }
        },
        {
          path: '/repair1',
          component: RepairView1,
          // meta: { requiresAuth: true, roles: ['repairer'] }
        }
      ]
    },
    {
      path: '/',
      name: 'home',
      component: HomeView,
      redirect:'index',
      children: [
        {
          path:'reap',
          component:ReapairerView,
          // meta: { requiresAuth: true, roles: ['admin'] }
        },
        {
          path:'/own',
          component:OwnerView,
          // meta: { requiresAuth: true, roles: ['admin'] }
        },
        {
          path:'/dept',
          component:DepartmentView,
          // meta: { requiresAuth: true, roles: ['admin'] }
        },
        {
          path:'/index',
          component:IndexView,
          // meta: { requiresAuth: true, roles: ['admin'] }
        },
        {
          path:'/repairTask',
          component:RepairTaskView,
          // meta: { requiresAuth: true, roles: ['admin'] }
        },
        {
          path:'/peijian',
          component:Peijian,
          // meta: { requiresAuth: true, roles: ['admin'] }
        }
      ]
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue')
    }
  ]
})
// router.beforeEach((to, from, next) => {
//   const isAuthenticated = sessionStorage.getItem('token') !== null;
//   const userRole = sessionStorage.getItem('role');
//   if (['/chooseLogin', '/login', '/repairerLogin'].includes(to.path)) {
//     next();
//   } else {
//     if (!isAuthenticated) {
//       // 如果需要认证但用户未登录，则重定向到登录页面  
//       next('/chooseLogin');
//     } else if (to.meta.requiresAuth && !to.meta.roles.includes(userRole)) {
//       // 如果需要特定角色但用户不是该角色，则重定向到错误页面或登录页面  
//       ElMessage.error("权限不足，无法访问！");
//       sessionStorage.removeItem('token');
//       sessionStorage.removeItem('role');
//       next('/chooseLogin');
//     } else {
//       // 验证通过，继续路由  
//       next();
//     }
//   }
// });
router.beforeEach((to, from, next) => {
  const tokenStore = useTokenStore();
  const isAuthenticated = tokenStore.tokenStr !== null;

  if (['/chooseLogin', '/login', '/repairerLogin'].includes(to.path)) {
    next();
  } else {
    if (!isAuthenticated) {
      // 如果需要认证但用户未登录，则重定向到登录页面  
      next('/chooseLogin');
    } else {
      // 验证通过，继续路由
      next();
    }
  }
});
export default router
